package presentacion;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;
import java.text.ParseException;

import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import javax.swing.JTextField;
import javax.swing.JButton;

import dominio.Billete;
import dominio.Cliente;
import dominio.Viaje;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Comprar_Billete {

	JFrame VentanaComprarBillete;
	private JPanel panel;
	private JLabel lblIdentificador;
	private JLabel lblPrecio;
	private JLabel lblIdViaje;
	private JLabel lblCliente;
	private JFormattedTextField ftxtDNI;
	private JTextField txtIdViaje;
	private JTextField txtId;
	private JLabel lblAsiento;
	private JTextField txtPrecio;
	private JTextField txtAsiento;
	private JButton btnAtras;
	private JButton btnComprar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Comprar_Billete window = new Comprar_Billete();
					window.VentanaComprarBillete.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Comprar_Billete() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		VentanaComprarBillete = new JFrame();
		VentanaComprarBillete.setTitle("Comprar billete");
		VentanaComprarBillete.setBounds(100, 100, 450, 157);
		VentanaComprarBillete.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		{
			panel = new JPanel();
			VentanaComprarBillete.getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(null);
			{
				lblIdentificador = new JLabel("Identificador:");
				lblIdentificador.setBounds(12, 13, 81, 16);
				panel.add(lblIdentificador);
			}
			{
				lblPrecio = new JLabel("Precio:");
				lblPrecio.setBounds(283, 45, 56, 16);
				panel.add(lblPrecio);
			}
			{
				lblIdViaje = new JLabel("Id. Viaje:");
				lblIdViaje.setBounds(273, 13, 56, 16);
				panel.add(lblIdViaje);
			}
			{
				lblCliente = new JLabel("Cliente:");
				lblCliente.setBounds(12, 45, 56, 16);
				panel.add(lblCliente);
			}
			{
				MaskFormatter formatoDNI;
				try {
					formatoDNI = new MaskFormatter("########'-U");
					formatoDNI.setPlaceholderCharacter('X');
					ftxtDNI = new JFormattedTextField(formatoDNI);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ftxtDNI.addFocusListener(new MiFocusListener());
				ftxtDNI.setBounds(59, 42, 81, 22);
				panel.add(ftxtDNI);
			}
			{
				txtIdViaje = new JTextField();
				txtIdViaje.setBounds(328, 10, 81, 22);
				panel.add(txtIdViaje);
				txtIdViaje.setColumns(10);
				txtIdViaje.addFocusListener(new MiFocusListener());
			}
			{
				txtId = new JTextField();
				txtId.setBounds(93, 10, 116, 22);
				panel.add(txtId);
				txtId.setColumns(10);
				txtId.addFocusListener(new MiFocusListener());
			}
			{
				lblAsiento = new JLabel("Asiento:");
				lblAsiento.setBounds(173, 45, 56, 16);
				panel.add(lblAsiento);
			}
			{
				txtPrecio = new JTextField();
				txtPrecio.setBounds(328, 42, 81, 22);
				panel.add(txtPrecio);
				txtPrecio.setColumns(10);
				txtPrecio.addFocusListener(new MiFocusListener());
			}
			{
				txtAsiento = new JTextField();
				txtAsiento.setBounds(223, 42, 36, 22);
				panel.add(txtAsiento);
				txtAsiento.setColumns(10);
				txtAsiento.addFocusListener(new MiFocusListener());
			}
			{
				btnAtras = new JButton("Atr\u00E1s");
				btnAtras.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Menu.VentanaMenu.setVisible(true);
						VentanaComprarBillete.setVisible(false);
					}
				});
				btnAtras.setBounds(312, 72, 97, 25);
				panel.add(btnAtras);
			}
			{
				btnComprar = new JButton("Comprar");
				btnComprar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
				
						try {
							Billete b=new Billete(Integer.parseInt(txtId.getText()),Double.parseDouble(txtPrecio.getText()), new Viaje(Integer.parseInt(txtIdViaje.getText()),null, null, null, null, null, null), new Cliente(ftxtDNI.getText(),null,null),txtAsiento.getText()); 
						if(b.insertar()==1){
								JOptionPane.showMessageDialog(VentanaComprarBillete, "Ha comprado el billete correctamente.");
								VentanaComprarBillete.setVisible(false);
								Menu.VentanaMenu.setVisible(true);
							}else{	
								JOptionPane.showMessageDialog(VentanaComprarBillete, "Error al comprar billete, vuelva a intentarlo.");
							}
						} catch (HeadlessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						};
				
					}
				});
				btnComprar.setBounds(43, 74, 97, 25);
				panel.add(btnComprar);
			}
		}
	}
	private class MiFocusListener extends FocusAdapter {
		@Override
		public void focusGained(FocusEvent e) {
			e.getComponent().setBackground(new Color(250, 250, 210));
		}

		@Override
		public void focusLost(FocusEvent e) {
			e.getComponent().setBackground(new Color(250, 250, 250));
		}
	}
}
